package com.spzx.product.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domian.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//product模块，前缀在网关中判断后 skip即可
@RestController
@RequestMapping("/brand")
@Tag(name = "品牌管理模块")
public class BrandController extends BaseController {

    @Resource
    private BrandService brandService;

    @Operation(summary = "条件查询品牌分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand)
    {
        startPage();
        List<Brand> list = brandService.selectRoleList(brand);
        return getDataTable(list);
    }
    @Operation(summary = "新增品牌")
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody Brand brand){
        if(!brandService.checkBrandNameUnique(brand)){
            //品牌名称被占用
            return error("新增品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.insertBrand(brand));
    }
    @Operation(summary = "删除品牌")
    @DeleteMapping("/remove/{brandId}")
    public AjaxResult remove(@PathVariable Long brandId){
        return toAjax(brandService.removeById(brandId));
    }
    @Operation(summary = "查询品牌详情")
    @GetMapping("/query/{brandId}")
    public AjaxResult getById(@PathVariable Long brandId){
        return success(brandService.getById(brandId));
    }
    @Operation(summary = "更新品牌")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody Brand brand){
        if(!brandService.checkBrandNameUnique(brand)){
            //品牌名称被占用
            return error("更新品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateBrand(brand));
    }


}